<template>
	<view>
		<view class="ALL">
			<view class="top">
				<text>注册信息 *必填</text>
			</view>
			<view class="title">
				<text>卡号 *</text>
				<input type="number" value="" placeholder="请输入卡号" @input="cardId" @blur="checkout('S',index)" />
				<text v-if="check==0"></text>
				<text v-if="check==34" id="check">卡号可以绑定</text>
				<text v-if="check==32" id="check1">卡号不能绑定</text>
				<text v-if="check==31" id="check1">卡号不存在</text>
				<text v-if="check==33" id="check1">可作为副卡</text>
			</view>
			<view class="line"></view>
			<view class="title">
				<text>用户姓名*</text>
				<input type="text" value=""  placeholder="请输入用户姓名" @input="userName" />
			</view>
			<view class="line"></view>
			<view class="title">
				<text>身份证号*</text>
				<input type="text" value=""  placeholder="请输入身份证号" @input="idCard" />
			</view>
			<view class="line"></view>
			<view class="title">
				<text>登录密码 *</text>
				<input type="password" value=""  placeholder="请输入密码" @input="passWord" />
			</view>
			<view class="line"></view>
			<view class="title">
				<text>邮箱</text>
				<input type="text" value=""  placeholder="请输入邮箱" @input="Email" />
			</view>
			<view class="line"></view>
			<view class="title">
				<text>手机*</text>
				<input type="number" value=""  placeholder="请输入手机" @input="Phone" />
			</view>
			<view class="line"></view>
			<view class="submit" @tap="submit">
				<text>确定注册</text>
			</view>
		</view>

	</view>
</template>

<script>
	import apis from '../../api.js'
	export default {
		data() {
			return {
				check: 0,
				requestUser: {
					username: '',
					documentno: '',
					email: '',
					mobile: '',
					cardid: '',
					password: '',
					code: '',
					location: {}
				},

			}
		},
		mounted:function(){
			this.getAuthorizeInfo();
			this.getLocationInfo();
		},
		onLoad() {
			var that = this
			/*
			uni.getLocation({
				type: 'gcj02',
				success: function(res) {
					//console.log('当前位置的经度：' + res.longitude);
					//console.log('当前位置的纬度：' + res.latitude);
					that.requestUser.location = res.latitude + "," + res.longitude
				}
			});
			*/
			uni.login({
				provider: 'weixin',
				success: function(loginRes) {
					// 获取用户信息
					//console.log(loginRes)
					that.requestUser.code = loginRes.code
					uni.getUserInfo({
						provider: 'weixin',
						success: function(infoRes) {
							// that.yonghuwx = infoRes.userInfo
							//console.log(infoRes)
						}
					});
				}
			});
		},

		methods: {
			verifyMobile:function (value) {
				return RegExp(/^1[34578]\d{9}$/).test(value);
			},
			verifyShenID:function (value) {
				return RegExp(/^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/).test(value);
			},
			getAuthorizeInfo(a="scope.userLocation"){  //1. uniapp弹窗弹出获取授权（地理，个人微信信息等授权信息）弹窗
				var _this=this;
				uni.authorize({
					scope: a,
					success() { //1.1 允许授权
						_this.getLocationInfo();
					},
					fail(){    //1.2 拒绝授权
						console.log("你拒绝了授权，无法获得周边信息")
					}
				})
			},
			getLocationInfo(){  //2. 获取地理位置
				var _this=this;
				uni.getLocation({
					type: 'wgs84',
					success (res) {
						console.log("你当前经纬度是：");
						console.log(res)
						_this.requestUser.location = res.latitude + "," + res.longitude
					}
				});
			},
			cardId(e) {
				this.requestUser.cardid = e.detail.value
			},
			passWord(e) {
				this.requestUser.password = e.detail.value
			},
			userName(e) {
				this.requestUser.username = e.detail.value
			},
			idCard(e) {
				this.requestUser.documentno = e.detail.value
			},
			Email(e) {
				this.requestUser.email = e.detail.value
			},
			Phone(e) {
				this.requestUser.mobile = e.detail.value
			},
			checkout(type, index) {
				var that = this
				if (type == 'S') {
					//console.log(this.requestUser.cardid)
					apis.verify({
						"code": that.requestUser.code,
						"cardid": that.requestUser.cardid
					}).then(res => {
						//console.log(res)
						this.check=res[1].data.status
						if(res[1].data.status==33){
							uni.showToast({
								icon:'none',
								title:'请登录后绑定副卡',
								duration: 2000
							})
						}
					})
				}
			},
			submit() {
				var that = this
					
				if (that.requestUser.cardid.length <= 0)
				{
					uni.showToast({
						title: '卡号不能为空,请输入卡号!',
						icon: "none"
					})
					return;
				}
				if (that.requestUser.username.length <= 0)
				{
					uni.showToast({
						title: '用户名不能为空,请输入用户名!',
						icon: "none"
					})
					return;
				}
				if (that.requestUser.documentno.length <= 0)
				{
					uni.showToast({
						title: '身份证不能为空,请输入身份证号!',
						icon: "none"
					})
					return;
				}
				if (that.requestUser.password.length <= 0)
				{
					uni.showToast({
						title: '登录密码不能为空,请输入登录密码!',
						icon: "none"
					})
					return;
				}
				if (that.requestUser.mobile.length <= 0)
				{
					uni.showToast({
						title: '手机号不能为空,请输入手机号码!',
						icon: "none"
					})
					return;
				}
				if(!that.verifyMobile(that.requestUser.mobile)){
					uni.showToast({
						title: '手机号输入不正确,请核对后重新输入!',
						icon: "none"
					})
					return;
				};	
				if(!that.verifyShenID(that.requestUser.documentno)){
					uni.showToast({
						title: '身份证号输入不正确,请核对后重新输入!',
						icon: "none"
					})
					return;
				};	
				apis.address({
					"location": that.requestUser.location
				}).then(res => {
					//console.log(res)
					uni.showModal({
						title: '当前地区为',
						content: res[1].data.message,
						success: function(res) {
							if (res.confirm) {
								//console.log('用户点击确定');
								apis.userRegistered({
									"username": that.requestUser.username,
									"documentno": that.requestUser.documentno,
									"email": that.requestUser.email,
									"mobile": that.requestUser.mobile,
									"cardid": that.requestUser.cardid,
									"password": that.requestUser.password,
									"code": that.requestUser.code,
									"location": that.requestUser.location
								}).then(res => {
									console.log(res)
									if(res[1].data.status == 200){
										uni.showModal({
											icon:'none',
											title:'注册成功,请重新登录!',
											showCancel: false,
											buttonText: '确定',
											success: (res) => {
												if (res.confirm) {
													console.log('用户点击确定');
													uni.navigateTo({
														url: '../login/login?Type=1&cardID=' + JSON.stringify(that.requestUser.cardid) + "&passWord="+ JSON.stringify(that.requestUser.password),
													})
												} else if (res.cancel) {
													console.log('用户点击取消');
												}
											}
										})										
									}
								})
							} else if (res.cancel) {
								//console.log('用户点击取消');
							}
						}
					});
				})

			}
		}
	}
</script>

<style scoped>
	.ALL {
		width: 100%;
		display: flex;
		flex-direction: column;
		align-items: center;
	}

	.top {
		width: 100%;
		height: 180rpx;
		text-align: center;
		line-height: 180rpx;
		color: #00B9EF;
		font-size: 34rpx;
		font-family: PingFang SC;
		font-weight: 500;
	}

	.title {
		width: 100%;
		height: 110rpx;
		display: flex;
		align-items: center;
	}

	.title>text {
		width: 25%;
		height: 110rpx;
		text-align: center;
		line-height: 110rpx;
		color: #000000;
		font-size: 30rpx;
		font-family: PingFang SC;
		font-weight: 400;
	}

	.title>input {
		width: 50%;
		height: 110rpx;
		line-height: 110rpx;
		font-size: 30rpx;
		font-family: PingFang SC;
		font-weight: 400;
	}

/* 	.line {
		width: 680rpx;
		height: 0rpx;
		border: 1rpx solid #cbcbcb;
		opacity: 1;
	} */

	.submit {
		width: 690rpx;
		height: 88rpx;
		background: #00B9EF;
		opacity: 1;
		border-radius: 10rpx;
		font-size: 34rpx;
		color: #FFFFFF;
		text-align: center;
		line-height: 88rpx;
	}

	#check {
		font-size: 20rpx;
		color: #00B9EF;
	}

	#check1 {
		font-size: 20rpx;
		color: #FF0000;
	}
</style>
